<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use think\app;
class Index extends Base
{
    // 系统信息
    private function get_sys_info(){
        if (extension_loaded('zip')) {
            $zip = "YES";
        } else {
            $zip = '<font color="red">NO（请开启 php.ini 中的php-zip扩展）</font>';
        }

        if(function_exists("gd_info")){
            $gd = gd_info();
            $gdinfo = $gd['GD Version'];
        }else {
            $gdinfo = "未知";
        }

        $mysqlinfo = Db::query("SELECT VERSION() as version");

        $sys_info = [
            ['name'=>'服务器操作系统','value'=>PHP_OS],
            ['name'=>'服务器环境','value'=>$_SERVER['SERVER_SOFTWARE']],
            ['name'=>'ThinkPHP版本','value'=>App::VERSION],
            ['name'=>'PHP 版本','value'=>phpversion()],
            ['name'=>'Mysql 版本','value'=>$mysqlinfo[0]['version']],
            ['name'=>'GD 版本','value'=>$gdinfo],
            ['name'=>'文件上传限制','value'=>@ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown'],
            ['name'=>'最大占用内存','value'=>ini_get('memory_limit')],
            ['name'=>'POST限制','value'=>@ini_get('file_uploads') ? ini_get('post_max_size') :'unknown'],
            ['name'=>'脚本最大执行时间','value'=>@ini_get("max_execution_time").'s'],
            ['name'=>'Zip支持','value'=>$zip],
            ['name'=>'Zlib支持','value'=>function_exists('gzclose') ? 'YES' : '<font color="red">NO（请开启 php.ini 中的php-zlib扩展）</font>'],
        ];
        return $sys_info;
    }

    //获取模块数据总量
    public function getHomeData(){
        $this->info['data']['sys_info'] = $this->get_sys_info();
        $this->info['data']['all_data'] = [
            ['name'=>'产品数量','count'=>Db::table('an_goods')->count(),'path'=>'/goods/list'],
            ['name'=>'资讯数量','count'=>Db::table('an_news')->count(),'path'=>'/news/list'],
            ['name'=>'留言数量','count'=>Db::table('an_form')->count(),'path'=>'/form_list'],
            ['name'=>'图片数量','count'=>Db::table('an_img')->count(),'path'=>'/img'],   
        ];
        $this->info['data']['log'] = Db::table('an_admin_log')
        ->alias('a')
        ->join(['an_admin'=>'b'],'a.admin_id = b.id')
        ->field('a.*,name')
        ->page(1,13)
        ->order('id desc')
        ->select();
        return json($this->info);
    }
    
    //上传图片到服务器
    public function upImg(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::putFile( 'uploads', $file);
        $this->info['code'] = 1;
        $this->info['msg'] = '上传成功';
        $this->info['data'] = str_replace("\\","/",$savename);
        return json($this->info);
    }

    //更新uniapp应用配置
    public function editUniappConfig(){
        $pt = input('post.');
        Db::table('an_config')->where('id',30)->update(['value'=>serialize($pt)]);
        return json($this->info);
    }

    //uniapp应用 是否审核模式
    public function editIsWxapp(){
        $pt = input('post.');
        Db::table('an_config')->where('id',29)->update(['value'=>$pt['is_wxapp']]);
        return json($this->info);
    }

    public function search(){
        $param = input('');
        $list = [];
        if($param['keyword']){
            $arr2 = Db::query("select productName title,id,'goods' as type from an_goods where productName  like '%{$param['keyword']}%' and a_show='1' ");
            $arr3 = Db::query("select title,id,'news' as type from an_news where title  like '%{$param['keyword']}%'");
            $list = array_merge($arr1,$arr2,$arr3,$arr4,$arr5,$arr6);
        }
        $this->info['data'] = $list;
        return json($this->info);
    }
}
